/*
 * MorseTest.java
 *
 * Created on May 15, 2008, 2:09 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package morseme;

/**
 *
 * @author tt
 */
import javax.microedition.lcdui.*;
import java.util.*;

public class MorseTest extends Form implements CommandListener{

private static Command transCmd = new Command("Translate", Command.OK, 1);
private static Command playCmd = new Command("Play", Command.OK, 1);
private static Command cancelCmd = new Command("Cancel", Command.CANCEL, 1);
private TextField tf = new TextField("Org : ", "", 160, TextField.ANY);
private StringItem sitem = new StringItem("Translated : ", "");
private MainMenu menu;
private String mStr = "";

public MorseTest(MainMenu menu) {

super("MorseME Testing");
this.menu = menu;

this.addCommand(transCmd);
this.addCommand(playCmd);
this.addCommand(cancelCmd);
this.setCommandListener(this);

this.append(tf);
this.append(sitem);

}

public void refresh(){
tf.setString("");
sitem.setText("");
}

public void commandAction(Command command, Displayable displayable) {

if(command == playCmd){
    
   

if(tf.getString().length() > 0){

if(sitem.getText().length() > 0){

sitem.setText("");
mStr = MorseProcess.translate(tf.getString());
sitem.setText(mStr);
}else{
mStr = MorseProcess.translate(tf.getString());
sitem.setText(mStr);
}



if(mStr != null){

for(int i = 0; i < Option.NUM_AUTO_PLAY; i++){
try{
if(i > 0){
Thread.sleep(20000);
}
MorseME.play(mStr);

}catch(InterruptedException ex){

ex.printStackTrace();
}
}
}
}

}else if(command == cancelCmd){

MorseME.stop();
Display.getDisplay(MorseME.getMIDlet()).setCurrent(menu);

}else if(command == transCmd){

if(tf.getString().length() > 0){

if(sitem.getText().length() > 0){

sitem.setText("");
mStr = MorseProcess.translate(tf.getString());
sitem.setText(mStr);
}else{
mStr = MorseProcess.translate(tf.getString());
sitem.setText(mStr);
}
}


}

}

public MorseTest() {
super("");
try {
jbInit();
}
catch(Exception e) {
}
}
void jbInit() throws Exception {
}
}

